package com.yuanian.ecsplus.bean.vo;

import com.yuanian.ecsplus.cache.CacheManager;
import com.yuanian.ecsplus.meta.entity.MetaEntity;
import com.yuanian.ecsplus.meta.vo.MetaVo;
import com.yuanian.ecsplus.model.entity.BaseModel;
import com.yuanian.ecsplus.model.vo.BaseVoModel;
import com.yuanian.ecsplus.page.PageSql;
import com.yuanian.ecsplus.page.Pagination;
import com.yuanian.ecsplus.platform.PlatformSupport;
import com.yuanian.ecsplus.platform.SpringContextBean;
import com.yuanian.ecsplus.session.PageSession;
import java.util.Map;

/**
 * 分页查询的model
 * @param <T>
 */
public class PageModel<T extends BaseModel> implements PageSession<T> {

    //查询实体类字节
    private Class<T> clz;

    //平台的支撑类
    private PlatformSupport platformSupport;

    public PageModel(Class<T> clz) {
        this.clz = clz;
        this.platformSupport = SpringContextBean.getBean(PlatformSupport.class);
    }

    /**
     * 返回分页数据
     * @param sql
     * @param limit
     * @param offset
     * @param orderBy
     * @return
     */
    public Pagination<T> selectList(String sql, int limit, int offset, String orderBy) {
        MetaEntity<T> metaEntity = CacheManager.getInstance().getMetaEntity(clz);
        return metaEntity.findList(sql, limit, offset, true, orderBy);
    }

    /**
     * 返回分页数据
     * @param sql
     * @param limit
     * @param offset
     * @param totalResults 是否包含总数量
     * @param orderBy
     * @return
     */
    public Pagination<T> selectList(String sql, int limit, int offset, boolean totalResults, String orderBy) {
        MetaEntity<T> metaEntity = CacheManager.getInstance().getMetaEntity(clz);
        return metaEntity.findList(sql, limit, offset, totalResults, orderBy);
    }

    @Override
    public Pagination<T> selectList(PageSql pageSql) {
        return selectList(pageSql.getSql(), pageSql.getLimit(), pageSql.getOffset(), true, pageSql.getOrderBy());
    }

    /**
     * 返回分页数据
     * @param sql
     * @param limit
     * @param offset
     * @param orderBy
     * @return
     */
    public Pagination<Map> selectMap(String sql, int limit, int offset, String orderBy) {
        /*MetaVo<T> metaEntity = CacheManager.getInstance().getMetaVo(clz);
        return metaEntity.findFieldListWithValueMap(sql, limit, offset, true, orderBy);*/
        return null;
    }

}
